﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Text.RegularExpressions;
using System.Threading.Tasks;

namespace System
{
    /// <summary>
    /// 表示常用的检测扩展方法
    /// </summary>
    /// <creator>marc</creator>
    public static class Check
    {
        /// <summary>
        /// 判断null
        /// </summary>
        /// <typeparam name="T"></typeparam>
        /// <param name="value">待判定的对象</param>
        /// <param name="paramName">参数名</param>
        /// <returns></returns>
        public static T NotNull<T>(this T value, string paramName = null)
        {
            if (value == null)
            {
                throw new ArgumentNullException(paramName ?? "The argument");
            }

            return value;
        }

        /// <summary>
        /// 判断null和empty
        /// </summary>
        /// <param name="value">待判定的对象</param>
        /// <param name="paramName">参数名</param>
        /// <returns></returns>
        public static string NotNullOrEmpty(this string value, string paramName = null)
        {
            if (string.IsNullOrEmpty(value))
            {
                throw new ArgumentException(paramName ?? "The argument", "String cannot be null or empty.");
            }

            return value;
        }

        /// <summary>
        /// 判断字符串是否为null或仅包含空白字符
        /// </summary>
        /// <param name="value">待判定的字符串</param>
        /// <param name="paramName">参数名</param>
        /// <returns></returns>
        public static string NotNullOrWhitespace(this string value, string paramName = null)
        {
            if (string.IsNullOrWhiteSpace(value))
            {
                throw new ArgumentException(paramName ?? "The argument", "String cannot be null, empty, or whitespace.");
            }

            return value;
        }

        /// <summary>
        /// 判断是否为true
        /// </summary>
        /// <param name="condition"></param>
        /// <param name="message">不为true的情况抛出的异常信息</param>
        public static void IsTrue(bool condition, string message = null)
        {
            if (!condition)
            {
                throw new ArgumentException(message ?? "The condition is false.");
            }
        }

        /// <summary>
        /// 判断<paramref name="value"/>不准为负数
        /// </summary>
        /// <param name="value">待判定的对象</param>
        /// <param name="paramName">参数名</param>
        public static void IsNotNegative(int value, string paramName = null)
        {
            if (value < 0)
            {
                throw new ArgumentOutOfRangeException(paramName ?? "The argument", "Value cannot be negative.");
            }
        }

        /// <summary>
        /// 判断数字是否在指定的范围内
        /// </summary>
        /// <param name="value">待判定的数字</param>
        /// <param name="min">最小值</param>
        /// <param name="max">最大值</param>
        /// <param name="paramName">参数名</param>
        public static void IsInRange(this int value, int min, int max, string paramName = null)
        {
            if (value < min || value > max)
            {
                throw new ArgumentOutOfRangeException(paramName ?? "The argument", $"Value must be between {min} and {max}. Current value: {value}");
            }
        }

        /// <summary>
        /// 判断集合是否为空
        /// </summary>
        /// <typeparam name="T">集合类型</typeparam>
        /// <param name="value">待判定的集合</param>
        /// <param name="paramName">参数名</param>
        public static void IsNotEmpty<T>(this IEnumerable<T> value, string paramName = null)
        {
            if (value == null || !value.Any())
            {
                throw new ArgumentException(paramName ?? "The argument", "Collection cannot be null or empty.");
            }
        }

        /// <summary>
        /// 判断数组是否为null或空
        /// </summary>
        /// <param name="value">待判定的数组</param>
        /// <param name="paramName">参数名</param>
        public static void IsNotNullOrEmpty(this Array value, string paramName = null)
        {
            if (value == null || value.Length == 0)
            {
                throw new ArgumentException(paramName ?? "The argument", "Array cannot be null or empty.");
            }
        }

        /// <summary>
        /// 判断数字是否不为零
        /// </summary>
        /// <param name="value">待判定的数字</param>
        /// <param name="paramName">参数名</param>
        public static void IsNotZero(this int value, string paramName = null)
        {
            if (value == 0)
            {
                throw new ArgumentException(paramName ?? "The argument", "Value cannot be zero.");
            }
        }

        /// <summary>
        /// 判断字符串是否为null、空、或仅包含空白字符
        /// </summary>
        /// <param name="value">待判定的字符串</param>
        /// <param name="paramName">参数名</param>
        /// <returns></returns>
        public static void IsNotNullOrEmptyOrWhitespace(this string value, string paramName = null)
        {
            if (string.IsNullOrWhiteSpace(value))
            {
                throw new ArgumentException(paramName ?? "The argument", "String cannot be null, empty, or whitespace.");
            }
        }

        /// <summary>
        /// 判断数字是否大于指定的值
        /// </summary>
        /// <param name="value">待判定的数字</param>
        /// <param name="minValue">最小值</param>
        /// <param name="paramName">参数名</param>
        public static void IsGreaterThan(this int value, int minValue, string paramName = null)
        {
            if (value <= minValue)
            {
                throw new ArgumentOutOfRangeException(paramName ?? "The argument", $"Value must be greater than {minValue}. Current value: {value}");
            }
        }

        /// <summary>
        /// 判断数字是否小于指定的值
        /// </summary>
        /// <param name="value">待判定的数字</param>
        /// <param name="maxValue">最大值</param>
        /// <param name="paramName">参数名</param>
        public static void IsLessThan(this int value, int maxValue, string paramName = null)
        {
            if (value >= maxValue)
            {
                throw new ArgumentOutOfRangeException(paramName ?? "The argument", $"Value must be less than {maxValue}. Current value: {value}");
            }
        }

        /// <summary>
        /// 判断手机号码是否合法
        /// </summary>
        /// <param name="phoneNumber">待判定的手机号码</param>
        /// <param name="paramName">参数名</param>
        public static void IsValidPhoneNumber(this string phoneNumber, string paramName = null)
        {
            if (!Regex.IsMatch(phoneNumber, @"^\d{10}$"))
            {
                throw new ArgumentException(paramName ?? "The argument", "Invalid phone number.");
            }
        }
    }
}
